﻿using System;
using System.Collections.Generic;
using System.Linq;

using KingsSharp.Core;
using KingsSharp.Core.Data;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Common;
using KingsSharp.Common.Extensions;

using CRM.Admin.Contracts.Contract.Sys;
using CRM.Admin.Contracts.Model.Sys;

namespace Property.Main.Services.Service.Sys
{
    public class OrganizationService : ServiceBase, IOrganizationService
    {
        /// <summary>
        /// 组织机构仓储
        /// </summary>
        private IRepository<Organization, Guid> OrganizationRepository;

        public OrganizationService(IRepository<Organization, Guid> _OrganizationRepository)
           : base(_OrganizationRepository.UnitOfWork)
        {
            this.OrganizationRepository = _OrganizationRepository;
        }

        public IQueryable<Organization> ItemOrganization
        {
            get { return OrganizationRepository.Entities; }
        }

        public KingsSharp.Common.Data.OperationResult InsertOrganization(params CRM.Admin.Contracts.Dto.Sys.OrganizationDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return OrganizationRepository.Insert(dtos, (dto) => {
                if (ItemOrganization.Where(a => a.Name == dto.Name && a.Parent.Id==dto.ParentOrganizationId).FirstOrDefault() != null)
                {
                    throw new Exception("同组织结构下 \"{0}\"已存在".FormatWith(dto.Name));
                }
                if (ItemOrganization.Where(a => a.Code == dto.Code).FirstOrDefault() != null)
                {
                    throw new Exception("组织结构代码 \"{0}\"已存在".FormatWith(dto.Code));
                }
            });
        }

        public KingsSharp.Common.Data.OperationResult UpdateOrganization(params CRM.Admin.Contracts.Dto.Sys.OrganizationDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return OrganizationRepository.Update(dtos, (dto) =>
            {
                if (ItemOrganization.Where(a => a.Name == dto.Name && a.Parent.Id == dto.ParentOrganizationId && a.Id != dto.Id).FirstOrDefault() != null)
                {
                    throw new Exception("同组织结构下 \"{0}\"已存在".FormatWith(dto.Name));
                }
                if (ItemOrganization.Where(a => a.Code == dto.Code && a.Id != dto.Id).FirstOrDefault() != null)
                {
                    throw new Exception("组织结构代码 \"{0}\"已存在".FormatWith(dto.Code));
                }
            });
        }
        /// <summary>
        /// 组织机构删除、非物理删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public KingsSharp.Common.Data.OperationResult DeleteOrganization(params Guid[] ids)
        {
            ids.CheckNotNull("ids");
            var entitys = ItemOrganization.Where(a => ids.Contains(a.Id)).ToList();
            entitys.ForEach(a => a.IsDeleted = true);
            int result = OrganizationRepository.Update(entitys.ToArray());
            return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
        }
    }
}
